/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import { useEffect } from 'react';
import { useDispatch } from 'umi';
import type { WinSize } from 'umi';

export function useWindowSizeChange() {
  // 按照antd.Grid组件分类
  const formatWinSize = (w: number): WinSize => {
    if (w >= 1600) return 'xxl';
    if (w >= 1200) return 'xl';
    if (w >= 992) return 'lg';
    if (w >= 768) return 'md';
    if (w >= 576) return 'sm';
    return 'xs';
  };

  const dispatch = useDispatch();

  useEffect(() => {
    const setWindowSize = () => {
      const w = document.documentElement.clientWidth;
      dispatch({
        type: 'global/changeWinSize',
        payload: formatWinSize(w),
      });
    };
    setWindowSize();
    window.addEventListener('resize', setWindowSize);
    return () => {
      window.removeEventListener('resize', setWindowSize);
    };
  }, []);
}
